﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassBuilder.Comment
{
    public static class Extensions
    {
        public static string FormatSummary(this string summary, bool firstIsSummary = true)
        {
            if (summary.NotEmpty())
            {
                if (summary.Length < 50 || summary.Count(c => c == '。' || c == '\n') <= 1)
                {
                    var lines = summary.Trim().Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                    return string.Join("\r\n", lines.Select(line => "/// " + line.Trim().HtmlHandle()));
                }
                else
                {
                    StringPlus plus = new StringPlus();
                    var lines = summary.Split(new string[] { "\r\n", "\n", "。", "；" }, StringSplitOptions.RemoveEmptyEntries).ToList();
                    if (firstIsSummary)
                    {
                        plus.AppendLine($"/// {lines.FirstOrDefault().Trim().HtmlHandle()}");
                        lines.RemoveRange(0, 1);
                    }
                    foreach (var line in lines)
                    {
                        plus.AppendLine("/// <para>");
                        plus.AppendLine($"/// {line.Trim().HtmlHandle()}");
                        plus.AppendLine("/// </para>");
                    }
                    return plus.ToString().Trim();
                }
            }
            return string.Empty;
        }
    }
}
